package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.OrderListItemsBean;
import com.wwp.tbshop.model.bean.OrderListItemsBean.OrderListBean;
import com.wwp.tbshop.util.OrderTimeFormatter;
import com.wwp.tbshop.util.PriceFormater;
import com.wwp.tbshop.util.SPUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.wwp.tbshop.R.id.orderId;


/**
 * Created by user on 2017/6/15.
 */

public class OrderListAdapter extends BaseAdapter {

    private static final String TAG = "OrderListAdapter";
    private List<OrderListBean> mOrderList;
    private Context mContext;
    private int listType;

    public OrderListAdapter(Context context, List<OrderListBean> orderList, int type) {
        this.mOrderList = orderList;
        mContext = context;
        this.listType = type;
    }

    @Override
    public int getCount() {
        return mOrderList.size();
    }

    @Override
    public Object getItem(int position) {
        return mOrderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemHolder itemHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.order_list_item,null);
            itemHolder = new ItemHolder(convertView);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }
        itemHolder.setData(mOrderList.get(position));
        return convertView;
    }

    public void setData(OrderListItemsBean orderListItemsBean, int type) {
        mOrderList = orderListItemsBean.orderList;
        notifyDataSetChanged();
    }


    class ItemHolder {
        @Bind(orderId)
        TextView mOrderId;
        @Bind(R.id.order_price)
        TextView mOrderPrice;
        @Bind(R.id.order_status)
        TextView mOrderStatus;
        @Bind(R.id.order_time)
        TextView mOrderTime;
        private OrderListBean mOrderListBean;

        ItemHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Log.e(TAG, "onClick: "+"ItemHolder");
                    Intent intent = new Intent("OrderDetail");
                    int userId = Integer.parseInt(SPUtils.getString(mContext,SPUtils.USERID,"0"));
                    intent.putExtra("userId",userId);
                    intent.putExtra("orderId",Integer.valueOf(mOrderListBean.orderId));
                    intent.putExtra("listType",listType);
                    mContext.startActivity(intent);

                }
            });
        }

        public void setData(OrderListBean orderListBean) {
            mOrderListBean = orderListBean;
            mOrderId.setText("订单编号: "+orderListBean.orderId);
            mOrderPrice.setText("订单总额: "+ PriceFormater.format(orderListBean.price)+"");
            mOrderStatus.setText("订单状态: "+orderListBean.status);
            mOrderTime.setText("下单时间: "+ OrderTimeFormatter.format(orderListBean.time));
        }
    }
}
